<?php
namespace Admin\Model;
use Think\Model;
class ProductsModel extends Model {
	 protected $autoCheckFields =false;
 
	//==============================产品操作
	public function listProducts($firstRow = 0, $listRows = 20,$wherearr='1=1') {
		$M = M("Products");
		$list = $M->where($wherearr)->order("`id` DESC")->limit("$firstRow , $listRows")->select();
		foreach ($list as & $v){
			$v['cid']=$this->getCname($v['cid']);
		}
		return $list;
	}
	
	public function catList() {
		 
		$cat = new \Admin\ORG\Category('Category', array('cid', 'pid', 'name', 'fullname'));
		$temp = $cat->getList();               //获取分类结构
	
		/*
		 for($i=0;$i<10;$i++){
		 $k=((I('get.p',1) - 1) * 10) + $i;
		 $temp[$k]['statusTxt'] =  $temp[$k]['status'] == 1 ? "启用" : "禁用";
		 $temp[$k]['chStatusTxt'] =  $temp[$k]['status'] == 0 ? "启用" : "禁用";
		 $temp[$k]['level'] = $level [$temp[$k]['level']];
		 $list[ $temp[$k]['id']] = $temp[$k];
		} */
		return $temp;
	}
   //============================产品分类
	
	public function addCategory() {
		$M = M("category");
		$data = $_POST['info'];
		$isexist=$M->where("pid=".$data['pid']." and name='".$data['name']."'")->count();
		if($isexist){
			return  array('status' =>3, 'info' => "分类名称已经存在", 'url' => U('Hotnew/category'));
		}
        if ($M->add($data)) {
			return array('status' => 1, 'info' => "发布成功", 'url' => U('Hotnew/category'));
		} else {
			return array('status' => 0, 'info' => "更新失败，请刷新页面尝试操作", 'url' => U('Hotnew/category'));
		}
	}
	
	public function editCategory() {
		$M = M("Category");
		$data = $_POST['info'];
		if ($M->save($data)) {
			return array('status' =>1, 'info' => "更新成功", 'url' => U('Hotnew/category'));
		} else {
			return array('status' => 0, 'info' => "更新失败，请刷新页面尝试操作",'url' => U('Hotnew/category'));
		}
	}
	public  function getCname($cid){
		$M=M("Category");
		$info=$M->where('cid='.$cid)->find();
		return $info['name'];
	}
    public function addProducts() {
        $M = M("Products");
        $postdata = I('post.');
        $data=$postdata['info'];
//             	$fff=M('category')->where('cid='.$data['cid'])->find();
// if ($fff['pid']!=0) {
//      $kk=M('category')->where('cid='.$fff['pid'])->find();
//      if (($kk['pid']==0)) {
//            $data['cid1']=$kk['cid'];
//      }else{
//     $data['cid2']=$kk['cid'];
//      }

//     if($kk['pid']!=0){
//         $kkh=M('category')->where('cid='.$kk['pid'])->find();
//                 $data['cid1']= $kkh['cid'];
//     }
// }
        if(!empty($_POST['picurl'])){
           $data['picurl']=serialize($_POST['picurl']);
        }
        $data["published"]=time();
        
        if ($M->add($data)) {
            return true;
        } else {
            return false;
        }
    }

    public function editProducts() {
        $M = M("Products");
        $postdata = I('post.');
        $data=$postdata['info'];
        if(!empty($_POST['picurl'])){
           $data['picurl']=serialize($_POST['picurl']);
        }else{

        	unset($data['picurl']);
        }
        if ($M->save($data)) {
            return ture;
        } else {
            return false;
        }
    }
     
}

?>
